<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>SOCKET TEST</title>
</head>
<body>
        <div>目前没有节目可以播放</div>
<script>
    var socketUrl = getQueryString("socketUrl");
    var webSocket = null;

    if(socketUrl){
        initWebSocket();
    }else{
        alert("未获取到socketUrl值");
    }

    function initWebSocket(){
        if ("WebSocket" in window) {
            webSocket = new WebSocket(socketUrl);
            webSocket.onopen = websocketonopen;
            webSocket.onmessage = websocketonmessage;
            webSocket.onerror = websocketonerror;
            webSocket.onclose = websocketclose;
        } else {
            console.log("当前浏览器不支持 websocket");
        }
    }

    function websocketonopen() {
        console.log("---连接建立成功---");
        //websocket连接后发送数据(send发送)
        websocketsend("connect success...");
    }

    function websocketonerror() {
        //连接建立失败重连
        console.log("-----websocket连接建立失败重连-----");
        initWebSocket();
    }

    function websocketonmessage(e) {
        //数据接收
        console.log("-----websocket接收到的发送的数据-----");
        console.log(e.data);
        alert(e);
        if(e.data){
            alert(e.data);
        }
    }

    function websocketsend(Data) {
        //数据发送
        console.log("发送数据", Data);
        webSocket.send(Data);
    }

    function websocketclose(e) {
        //关闭
        console.log("断开连接");
    }

    function closeWebsocket(){
        webSocket.close();
    }

    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return r[2];
        return null;
    }
</script>
</body>
</html>
